<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Shijue_bu extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	function __construct() {
		parent::__construct();
		//ignore_user_abort();//关掉浏览器，PHP脚本也可以继续执行.
		//set_time_limit(3000);// 通过set_time_limit(0)可以让程序无限制的执行下去
		//$interval=5;// 每隔5s运行
		date_default_timezone_set('Asia/Shanghai'); 

		
	}


	public function add_renwu(){
		for ($x=0; $x<=10; $x++) {
  		echo $this->home();
		} 
	}
	public function home()
	{	

	
		$sql = "select * from task_bulu";
		$sh = $this->db->query($sql);
        	$data = $sh->result_array();
		foreach ($data as $v) {
			$arr = array();
			$arr['task_id'] = $v['task_id'];
			$arr['clue_id'] = $v['clue_id'];
			$query = $this->db->get_where('shijue_project_new', $arr);
			$users = $query->row_array();
			if(!$users){
				$clue_data = array();
				$clue_data['task_id']=$v['task_id'];
				$clue_data['company_id']=$v['company_id'];
				$clue_data['clue_id']=$v['clue_id'];
				$clue_data['clue_address']=$v['clue_address'];
				$clue_data['clue_type']=$v['clue_type'];
				$clue_data['is_priority']=1;
				if($v['clue_type'] == 2){
					$rs = $this->pr_biz($clue_data['clue_address']);
					$clue_data['biz'] =$rs;
				}
				if($v['clue_type'] == 3){
					$url = trim($v['clue_address']);
					$rs = strpos($url,"m.weibo");
					if($rs){
    						$str =substr($url,-10); 
    						$clue_data['biz'] = $str;
					}else{
						$rs = $this->send_http(trim($url));
						preg_match_all("|CONFIG\['oid'\]='(.*?)';|ims", $rs, $r);
						$data=$r[1][0];
                                       		$clue_data['biz'] =$data;
					}	
                         	}	
				$rrr = $this->db->insert('shijue_project_new',$clue_data);
				echo $rrr;
				echo "\n";
				echo "+++++++++++++++++++++++++++++++++";
				echo "\n";
			

			}else{
				if($v['status'] == 3 && $v['spider_num'] >0){
					echo "此任务已完成，无需更改";
								
				}
				if($v['status'] == 2 && $v['spider_num'] > 0){
					echo "此任务正在继续，无需更改";
				}
				if($v['status'] == 1){
					echo "此任务没有需要添加";
					$clue_data = array();
                                	$clue_data['task_id']=$v['task_id'];
                                	$clue_data['company_id']=$v['company_id'];
                                	$clue_data['clue_id']=$v['clue_id'];
                                	$clue_data['clue_address']=$v['clue_address'];
                                	$clue_data['clue_type']=$v['clue_type'];
                                	$clue_data['is_priority']=1;
                                	if($v['clue_type'] == 2){
                                        	$rs = $this->pr_biz($clue_data['clue_address']);
                                        	$clue_data['biz'] =$rs;
                                	}
                                	if($v['clue_type'] == 3){
                                        	$url = trim($lists['clue_address']);
                                        	$rs = strpos($url,"m.weibo");
                                        	if($rs){
                                                	$str =substr($url,-10);
                                                	$clue_data['biz'] = $str;
                                        	}else{
                                                	$rs = $this->send_http(trim($url));
                                                	preg_match_all("|CONFIG\['oid'\]='(.*?)';|ims", $rs, $r);
                                                	$data=$r[1][0];
                                                	$clue_data['biz'] =$data;
                                        	}
                                	}
                                	$rrr = $this->db->insert('shijue_project_new',$clue_data);
                                	echo $rrr;
                                	echo "\n";
                                	echo "+++++++++++++++++++++++++++++++++";
                                	echo "\n";
				}
				
	
	

			}

		
			
		}
	}
	
	
	public function pl_biz(){
	$rs = "SELECT * FROM shijue_project_new WHERE clue_type = 2";
	$sh = $this->db->query($rs);
        $data = $sh->result_array();
	//print_r($data);
	foreach($data as $v){
		$s = $this->pr_biz($v['clue_address']);
		$clue_data['biz'] =$s;
		$where = array(
			'id'=>$v['id']
		);
		$r = $this->db->update('shijue_project_new', $clue_data, $where);
		echo $r;	
	}	
	
	
	}	
	
	public function pr_biz($str){
		if( preg_match('|biz=(.*?)&|ims', $str, $out)) {
			return $out[1];
		}
	}


function send_http($url, $post = array(), $ip = "", $timeout = 15, $header = array(), $connecttimeout = 15) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

     $cookie = "SUB=_2A250_Ua0DeThGeBN4loV8SvJzzSIHXVXiz98rDV8PUNbmtBeLRPikW86-ey9AyJvjSMRjv0I0d-q8mNPKw..;";
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    
    if($post){
        if(is_array($post)){
            $post = http_build_query($post);
        }

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }

    if ($header){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    
    if(!empty($ip)) {
        curl_setopt($ch, CURLOPT_PROXY, $ip);
        curl_setopt($ch, CURLOPT_PROXYTYPE , CURLPROXY_SOCKS5);
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "hongmai:soften.cn");
    }

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
    
    $rs = curl_exec($ch);
    $http_info = curl_getinfo($ch);
    //print_r($http_info);
    curl_close($ch);
    if($http_info['http_code'] != 200 && $http_info['http_code'] != 302 ){
        //print_r($http_info);
        return false;
    }
    return $rs;
}
	public function send_post($url, $post_data = '', $timeout = 5){  
        	$ch = curl_init();  
        	curl_setopt ($ch, CURLOPT_URL, $url);  
        	curl_setopt ($ch, CURLOPT_POST, 1);  
        	if($post_data != ''){  
            		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
        	}  
        	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
        	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
        	curl_setopt($ch, CURLOPT_HEADER, false);  
        	$file_contents = curl_exec($ch);  
        	curl_close($ch);  
        	return $file_contents;  
    	}
	public function add_weixin_data()
	{
		$rs_weixin= $this->db->where('clue_type',2)->get('weixin_re');
        $data =$rs_weixin->result_array();
        //print_r($data);die;
        foreach ($data as $v) {
        	$weixin_data = array();
	        $ou = '|biz=(.*?)&|ims';
	        preg_match_all($ou,$v['clue_address'],$os,PREG_PATTERN_ORDER);
	        //print_r($os);die;
	        $weixin_data['biz'] = $os[1][0];
	        echo $rs = $this->db->where('id',$v['id'])->update('weixin_re',$weixin_data);
	      
        }		
	
	}
	
	function get_url($url)   
	{  
	    $ch = curl_init();  
	    curl_setopt($ch, CURLOPT_URL,$url);  //设置访问的url地址     
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);      
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容  
	    $result =  curl_exec($ch);  
	    curl_close ($ch);  
	    return $result;   
	}  
}	
	
